<?php
/*
 *  supplier 供货商添加类
 *
 *  @param $c 一般是类名
 *  @param $a 一般是方法名
 */
class product extends Admin {

   // private $tab = array( 'goodsEdit' => '宝贝编辑', 'goodsTask' => '宝贝任务');
    private $prePicAddr = "content/attachment/prePic";
    private $postPicAddr = "content/attachment/postPic";
    private $unzipPicAddr = "content/attachment/unzipPic";
    private $goodsHtmlAddr = "content/goodsHtml";
    private $goodsNo = '';
    private $pagePicInfo='';
    private $goodsRight =array('宝贝定价'=>1,'自然属性'=>2,'宝贝原图'=>3,'宝贝命名'=>4,'宝贝尺寸'=>5,'处理后图片'=>6,'宝贝描述'=>7);

    public function index( $c, $a ) {
        $this->table = 'bsInfo';

        $where = '';

        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        include $this->display( $c );
    }

    public function  search($c,$a)
    {
        $this->table = 'bsInfo'; // 数据库表名

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "id ={$key} " ) : '';
        }

        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];


        include $this->display( $c ); // 加载模板
    }
    public function edit( $c, $a ) {
        if ( !$id = intval( G( 'id' ) ) ) {
            $this->error();
            exit;
        }

        $this->table = ''; // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function editAttr( $c, $a)
    {

       $goodsNo = intval( G( 'goodsNo' ));

       $condition = array('goodsNo'=>$goodsNo);
       $this->table = 'natureAttr';
        $this->pk = 'goodsNo';
       $result = $this->findAll($condition);



       if(isset($result))
       {
           $result = array('goodsNo'=>$goodsNo);
       }

       print_r($result);
       include $this->display( $c );
    }
    public function editOrigPic($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));

        $condition = array('goodsNo'=>$goodsNo);
        $this->table = 'natureAttr';
        $this->pk = 'goodsNo';
        $result = $this->find($condition);



        if(isset($result))
        {
            $result = array('goodsNo'=>$goodsNo);
        }

        print_r($result);
        include $this->display( $c );
    }
    public function editPrice($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));

        $condition = array('goodsNo'=>$goodsNo);
        $this->table = 'natureAttr';
        $this->pk = 'goodsNo';
        $result = $this->find($condition);



        if(isset($result))
        {
            $result = array('goodsNo'=>$goodsNo);
        }

        print_r($result);
        include $this->display( $c );
    }
    public function editGoodsName($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));
        $result = array('goodsNo'=>$goodsNo);
        include $this->display( $c );
    }
    public function editClothSize($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));
        $result = array('goodsNo'=>$goodsNo,'t'=>intval( G( 't' )));
        print($result['t']);
        include $this->display( $c );
    }
    public function editPostPic($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));
        $result = array('goodsNo'=>$goodsNo);
        include $this->display( $c );
    }
    public function editOnSaleDes($c, $a)
    {
        $goodsNo = intval( G( 'goodsNo' ));
        $templateId = 1;
        $this->table = 'templateInfo';
        $result1 = $this->findAll(array('templateId'=> $templateId,'pagePicFlag'=>true),'pagePosition',"pagePicName");

        for($i = 0; $i <count($result1); $i++ )
        {

            $result[$i] = $result1[$i]['pagePicName'];
        }

        print_r($result);

        include $this->display( $c );
    }
    private function unzip( $zipFile )
    {
        $zip = new ZipArchive();
        $res = $zip->open( $zipFile );
        if ( $res !== true ) {
            $zip->close();
            return '系统错误:' . $res;
        }

        $msg = $zip->extractTo( WEBROOT . 'content/unzip/' ) ? true : '解压文件失败!';
        $zip->close();
        return $msg;

    }
    public function  updateGoodsStatus($status)
    {
        $this->table = 'bsInfo';
        $rows['goodsStatus'] = $status;

        $msg = $this->update( array('id'=>$_POST['goodsNo']), $rows ) ? 'success' : 'failed';  //更新供货商
    }



    public function saveEditAttr( $c, $a ) {
        # 获取表单数据

        //print_r($_POST);
        foreach ( $_POST as $key => $val ) {

            if(('size' == $key)||('color' == $key))
            {
                if('size' == $key)
                {
                    $rows['size'] = implode(",",$_POST['size']);
                }
                else
                {
                    $rows['color'] = implode(",",$_POST['color']);
                }

            }
            else
            {
                $rows[ $key ] = htmlspecialchars( trim( $val ) );
            }

        }

        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = 'natureAttr'; // 数据库表名
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        unset($rows);
        $rows['goodsNo'] = $_POST['goodsNo'];
        $rows['goodsStatus'] = 1;                    //'1':宝贝编辑
        $rows['idNatureAttr'] = $_SESSION['userId'];


        $this->updateGoodsStatus($this->goodsRight['自然属性']);

        /**zouhong-edit**/
      //  $rows['dateNatureAttr'] = date("Y-M-d");
        /**zouhong-edit-end**/

       $this->table = 'goodsEdit';
       $rows['idNatureAttr'] = $this->admin['id'];
       $rows['dateNatureAttr'] = date("Y-M-D");
       $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        exit( $msg );
    }

    public function saveEditOrigPic( $c, $a ) {

        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        $rows[ 'dateline' ] = strtotime( $rows[ 'dateline' ] );
        unset( $rows[ 'attach' ] );

        print_r($_FILES);

        echo "Type: " . $_FILES["prePicfile"]["type"] . "<br />";

        # 注意 表单字段必须跟数据库表字段名一样
        if (true)
        {
            if ($_FILES["prePicfile"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {

                if (file_exists($this->prePicAddr.date("Y-m-d").$_FILES["prePicfile"]["name"]))
                {
                    echo $_FILES["prePicfile"]["name"] . " already exists. ";
                    return;
                }
                else
                {
                    if(!file_exists($this->prePicAddr .'/'.date("Y-m-d")))
                    {
                        mkdir($this->prePicAddr .'/'.date("Y-m-d"));
                    }

                    print_r(WEBROOT .$this->prePicAddr .'/'.date("Y-m-d").'/'. $_FILES["prePicfile"]["name"]);
                     move_uploaded_file($_FILES["prePicfile"]["tmp_name"],
                      WEBROOT. $this->prePicAddr .'/'.date("Y-m-d").'/'.$_POST['goodsNo'].'rar');


                }
            }
        }
        else
        {
            echo "Invalid file";
        }


                   $zip = new ZipArchive();

                   $res = $zip->open(  $this->prePicAddr .'/'.date("Y-m-d").'/'. $_POST['goodsNo'].'rar' );
                   if ( $res !== true ) {

                       $zip->close();
                       return '系统错误:' . $res;
                   }

                   $msg = $zip->extractTo( WEBROOT . $this->unzipPicAddr ) ? true : '解压文件失败!';
                   $zip->close();


    $this->updateGoodsStatus($this->goodsRight['宝贝原图']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['datePhoto'] = $this->admin['id'];
        $rows['idNatureAttr'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        exit( $msg );


    }


    public function saveEditPrice( $c, $a ) {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        # 注意 表单字段必须跟数据库表字段名一样
        /**zouhong-edit**/
        $rows['id'] = 0;
        /**zouhong-edit-end**/
        $this->table = 'priceDescribe'; // 数据库表名

        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        unset($rows);



        $this->updateGoodsStatus($this->goodsRight['宝贝定价']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['idPrice'] = $this->admin['id'];
        $rows['datePrice'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }
        exit($msg);
    }



    public function saveGoodsName( $c, $a ) {

        # 获取表单数据

        $this->table = 'natureAttr'; // 数据库表名

        $goodsAttr = array('1'=>'clothType','2'=>'brandName','3'=>'picType','4'=>'thickness','5'=>'flexAttr','6'=>'style');
        $proKey = array('1'=>'反季促销','2'=>'新店开张','3'=>'清仓促销','4'=>'秋冬新品','5'=>'春夏新装','6'=>'包邮促销');


        $this->pk = 'goodsNo';
        $result = $this->find(array('goodsNo'=>$_POST['goodsNo']));

        $arrayGoodsAttr = $_POST['goodsAttr'] ;

        foreach($arrayGoodsAttr as  $key => $val)
        {
               print_r($val);
               print_r($goodsAttr[$val]);
               $name = $name . $result[$goodsAttr[$val]];
        }
        print_r($name);
        $name = $name.$proKey[$_POST['proKey']];



        //"select A.id,A.[name] as sName,B.[address] AS sAddress from info A,place B where A.id=B.id";


        //$this->table = 'bsInfo';
        $msg = $this->update(array('goodsNo'=>$_POST['goodsNo']),array('goodsName'=>$name));



        $this->updateGoodsStatus($this->goodsRight['宝贝命名']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['idName'] = $this->admin['id'];
        $rows['dateName'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }


        exit( $msg );

    }

    public function saveEditPostPic( $c, $a ) {

        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }

        $rows[ 'dateline' ] = strtotime( $rows[ 'dateline' ] );
        unset( $rows[ 'attach' ] );

        print_r($_FILES);

        echo "Type: " . $_FILES["postPicfile"]["type"] . "<br />";

        # 注意 表单字段必须跟数据库表字段名一样
        if (true)
        {
            if ($_FILES["postPicfile"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {

                if (file_exists($this->postPicAddr.date("Y-m-d").$_FILES["postPicfile"]["name"]))
                {
                    echo $_FILES["postPicfile"]["name"] . " already exists. ";
                    return;
                }
                else
                {
                    if(!file_exists($this->postPicAddr .'/'.date("Y-m-d")))
                    {
                        mkdir($this->postPicAddr .'/'.date("Y-m-d"));
                    }

                    print_r(WEBROOT .$this->postPicAddr .'/'.date("Y-m-d").'/'. $_FILES["postPicfile"]["name"]);
                    move_uploaded_file($_FILES["postPicfile"]["tmp_name"],
                        WEBROOT. $this->postPicAddr .'/'.date("Y-m-d").'/'.$_POST['goodsNo'].'.rar');


                }
            }
        }
        else
        {
            echo "Invalid file";
        }


        $zip = new ZipArchive();

        $res = $zip->open(  $this->postPicAddr .'/'.date("Y-m-d").'/'. $_POST['goodsNo'].'rar' );
        if ( $res !== true ) {

            $zip->close();
            return '系统错误:' . $res;
        }

        $msg = $zip->extractTo( WEBROOT . $this->unzipPicAddr ) ? true : '解压文件失败!';
        $zip->close();

        $this->updateGoodsStatus($this->goodsRight['处理后图片']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['idPS'] = $this->admin['id'];
        $rows['datePS'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

        return $msg;


    }
    public function saveEditOnSaleDes( $c, $a )
    {
        if(!file_exists($this->goodsHtmlAddr .'/'.date("Y-m-d")))
        {
            mkdir($this->goodsHtmlAddr .'/'.date("Y-m-d"));
        }

        /**查询模版文件信息***/

        $sql = "SELECT * FROM xbl_templateinfo WHERE pagePicName IS NOT NULL AND templateId = 1 ORDER BY pagePosition asc ";
        $res = $this->findSql($sql);

        /**创建宝贝描述文件**/
        $file=fopen($this->goodsHtmlAddr .'/'.date("Y-m-d").'/'.$_POST['goodsNo'],"w+") or exit("Unable to open file!");


        fwrite($file,"<table cellSpcing=0 cellPadding=0 width=720 border=0><br/>");


        $j = 0;
        for($t = 0;  $t<count($res); $t++)
        {
            fwrite($file,"<tr><TD> <img src = " .'"' .$res[$t]['pagePicUrl'].'"'." width = 720> </td></tr>");
            if(1 == $res[$t]['pagePicFlag'])
            {
                for($i = 0 ; $i < 4; $i++)
                {
                    print_r($_POST["pic$j$i"]);
                    if(''!=$_POST["pic$j$i"])
                        fwrite($file,"<tr><td> <img src = " .'"' .$_POST["pic$j$i"].'"'." width = 720> </td></tr><br/>");
                }
                $j++;
            }

        }

        fwrite($file,"</table>");
        fclose($file);




        $this->updateGoodsStatus($this->goodsRight['宝贝描述']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['idgoodEidt'] = $this->admin['id'];
        $rows['dateGoodsEdit'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }




    }
    public function saveEditClothSize( $c, $a ) {
        foreach ( $_POST as $key => $val ) {
                $rows[ $key ] = htmlspecialchars( trim( $val ) );
            }

        $this->table = 'clothSize'; // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $msg = $this->update( array( 'id' => $id ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }


        $this->updateGoodsStatus($this->goodsRight['宝贝尺寸']);

        /**更新宝贝编辑表**/
        $this->table = 'goodsEdit';
        $rows['idSize'] = $this->admin['id'];
        $rows['dateSize'] = date("Y-M-D");
        $this->pk = 'goodsNo';

        if ( $id = $this->find(  array('goodsNo' => $rows[ 'goodsNo' ])) ) {
            $msg = $this->update( array( 'goodsNo' => $id['goodsNo'] ), $rows ) ? 'success' : 'failed';  //更新供货商
        } else {
            $msg = $this->create( $rows ) ? 'success' : 'failed';                         // 添加供货商
        }

    }







}